package com.aaa.service.sys.impl;

import com.aaa.dao.sys.UserDao;
import com.aaa.pojo.Dept;
import com.aaa.pojo.PageSort;
import com.aaa.pojo.Userinfo;
import com.aaa.service.sys.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

  /*  @Override
    public List<Userinfo> findAllUser(String page, String limit) {
        int pageInt = Integer.parseInt(page);
        int limitInt = Integer.parseInt(limit);
        int start = (pageInt - 1) * limitInt;
        List<Userinfo> user = userDao.findAllUser(start, limitInt);
        return user;
    }*/
  @Override
  public Page findAllUser(PageSort pageSort) {
      // 开始分页
      PageHelper.startPage(pageSort.getPage(), pageSort.getLimit());
      // 分页+模糊查询+排序 查询
      List<Userinfo> userinfoList = userDao.findAllUser(pageSort);
      Page listWithPage = (Page) userinfoList;
      return listWithPage;
  }

    public List<Map<String, Object>> findAllUserInfoByName(Map map) {

      return userDao.findAllUserInfoByName(map);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean addUser(Userinfo userinfo){
        int num = 0;
        // 添加新用户,并返回主键（即当前添加用户的ID）
        int i = userDao.addUser(userinfo);
        if (i > 0) {
            // 获取用户id
            List<Userinfo> userinfoList = userDao.findUserIdByName(userinfo.getUsername());
            Integer userid = userinfoList.get(0).getUserid();
            // 获取新用户的角色id
            Integer roleId = userinfo.getRoleId();
            // 向中间表tbl_user_role添加相应信息
            System.out.println(userid + "----" + roleId);
            num = userDao.addUserRole(userid, roleId);
        }
        if (num > 0){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteUserById(String[] userids) {
        int num = userDao.deleteUserById(userids);
        if (num > 0){
            return true;
        }
        return false;
    }

    public List<Dept> queryAllDept() {
        return userDao.queryAllDept();
    }

    @Override
    public boolean resetPassword(Userinfo userinfo) {
        int num = userDao.resetPassword(userinfo);
        if (num > 0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateUser(Userinfo userinfo) {
        int num = userDao.updateUser(userinfo);
        if (num > 0){
            return true;
        }
        return false;
    }

    @Override
    public boolean findNoSameName(Userinfo userinfo) {
        // 查找不同名字
        List<Userinfo> userinfoList = userDao.findNoSameName(userinfo);
        if (userinfoList != null && userinfoList.size() > 0){
            // 有重名
            return false;
        }
        // 没有相同
        return true;
    }

    @Override
    public boolean updateOneself(Userinfo userinfo) {
        int num = userDao.updateOneself(userinfo);
        if (num > 0){
            return true;
        }
        return false;
    }



}
